package repository

import (
	"energy-amiot-platform/model/dbspo"

	"gorm.io/gorm"
)

var (
	APP appRepo
)

type appRepo struct {
}

func (p *appRepo) FindByKey(db *gorm.DB, key uint64) (*gorm.DB, *dbspo.App) {
	var app dbspo.App
	tx := db.First(&app, "id=?", key)
	return tx, &app
}

func (p *appRepo) Exist(db *gorm.DB, key uint64) (*gorm.DB, bool) {
	var count int64
	tx := db.Model(&dbspo.App{}).Where("id=?", key).Count(&count)
	return tx, count > 0
}

func (p *appRepo) ExistWithCompanyIdAndAppName(db *gorm.DB, companyId uint64, name string) (*gorm.DB, bool) {
	var count int64
	tx := db.Model(&dbspo.App{}).Where("cid = ? and name = ?", companyId, name).Count(&count)
	return tx, count > 0
}

func (p *appRepo) CountByCid(db *gorm.DB, companyId uint64) (*gorm.DB, *int64) {
	var count int64
	return db.Model(&dbspo.App{}).Where("cid=?", companyId).Count(&count), &count
}

func (p *appRepo) Delete(db *gorm.DB, app *dbspo.App) *gorm.DB {
	return db.Delete(app)
}

func (p *appRepo) Create(db *gorm.DB, item *dbspo.App) *gorm.DB {
	return db.Create(item)
}
